﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Web.UI.WebControls;

namespace Axe.Web.Controls
{
    using Axe.Extensions;
    using System.Collections.ObjectModel;
    using System.Web.UI;

    public class HyperLinkList : DataBoundControl, IRepeatInfoUser
    {
        private HyperLink controlToRepeat;
        private HyperLinkItemCollection _items;

        #region private HyperLink ControlToRepeat
        private HyperLink ControlToRepeat
        {
            get
            {
                if (controlToRepeat == null)
                    controlToRepeat = new HyperLink();
                return controlToRepeat;
            }
        }
        #endregion


        public virtual string DataTextField
        {
            get { return ViewState.Get("DataTextField", ""); }
            set { ViewState["DataTextField"] = value; }
        }

        public virtual string DataTooltipField
        {
            get { return ViewState.Get("DataTooltipField", ""); }
            set { ViewState["DataTooltipField"] = value; }
        }

        public virtual string DataUrlField
        {
            get { return ViewState.Get("DataUrlField", ""); }
            set { ViewState["DataUrlField"] = value; }
        }

        public virtual HyperLinkItemCollection Items
        {
            get
            {
                if (_items == null)
                {
                    _items = new HyperLinkItemCollection();
                    if (base.IsTrackingViewState)
                        _items.TrackViewState();
                }
                return _items;
            }
        }

        protected override object SaveViewState()
        {
            object baseState = base.SaveViewState();
            object itemState = Items.SaveViewState();
            return new Pair(baseState, itemState);
        }

        protected override void LoadViewState(object savedState)
        {
            if (savedState != null)
            {
                Pair p = savedState.Cast<Pair>();
                base.LoadViewState(p.First);
                Items.LoadViewState(p.Second);
            }
        }

        #region IRepeatInfoUser Members

        Style IRepeatInfoUser.GetItemStyle(ListItemType itemType, int repeatIndex)
        {
            return null;
        }

        public bool HasFooter
        {
            get { return false; }
        }

        public bool HasHeader
        {
            get { return true; }
        }

        public bool HasSeparators
        {
            get { return false; }
        }

        public void RenderItem(ListItemType itemType, int repeatIndex, RepeatInfo repeatInfo, HtmlTextWriter writer)
        {
            HyperLink ctl = ControlToRepeat;
            int i = repeatIndex;
            if (i >= 0)
            {
                ctl.ID = i.ToString();
                ctl.Text = Items[i].Text;
                ctl.ToolTip = Items[i].Tooltip;
                ctl.NavigateUrl = Items[i].Url;
                ctl.RenderControl(writer);
            }
        }

        int IRepeatInfoUser.RepeatedItemCount
        {
            get { return this.Items.Count; }
        }

        #endregion

        public virtual RepeatDirection RepeatDirection
        {
            get { return ViewState.Get("RepeatDirection", RepeatDirection.Vertical); }
            set { ViewState["RepeatDirection"] = value; }
        }

        public virtual int RepeatColumns
        {
            get { return ViewState.Get<int>("RepeatColumns", 0); }
            set { ViewState["RepeatColumns"] = value; }
        }

        public virtual RepeatLayout RepeatLayout
        {
            get { return ViewState.Get("RepeatLayout", RepeatLayout.Table); }
            set { ViewState["RepeatLayout"] = value; }
        }

        #region protected override void PerformDataBinding(System.Collections.IEnumerable data)
        protected override void PerformDataBinding(System.Collections.IEnumerable data)
        {
            base.PerformDataBinding(data);
            string urlField = DataUrlField;
            string textField = DataTextField;
            string tooltipField = DataTooltipField;

            if (data != null)
            {
                foreach (object o in data)
                {
                    HyperLinkItem item = new HyperLinkItem();
                    item.Url = DataBinder.GetPropertyValue(o, urlField, null);
                    item.Text = DataBinder.GetPropertyValue(o, textField, null);
                    item.Tooltip = DataBinder.GetPropertyValue(o, tooltipField, null);
                    Items.Add(item);
                }
            }
        } 
        #endregion

        #region protected override void Render(HtmlTextWriter writer)
        protected override void Render(HtmlTextWriter writer)
        {
            if (Items.Count > 0)
            {
                RepeatInfo ri = new RepeatInfo();
                Style controlStyle = (base.ControlStyleCreated ? base.ControlStyle : null);
                ri.RepeatColumns = this.RepeatColumns;
                ri.RepeatDirection = this.RepeatDirection;
                ri.RepeatLayout = this.RepeatLayout;
                ri.RenderRepeater(writer, this, controlStyle, this);
            }
        } 
        #endregion
    }

    public class HyperLinkItem
    {
        public HyperLinkItem() { }
        public HyperLinkItem(string text, string url, string tooltip)
        {
            this.Text = text;
            this.Url = url;
            this.Tooltip = tooltip;
        }

        public string Text { get; set; }
        public string Url { get; set; }
        public string Tooltip { get; set; }
    }

    public class HyperLinkItemCollection : Collection<HyperLinkItem>, IStateManager
    {
        private bool _marked;

        public HyperLinkItemCollection()
        {
            _marked = false;
        }

        #region IStateManager Members

        public bool IsTrackingViewState
        {
            get { return _marked; }
        }

        public void LoadViewState(object state)
        {
            if (state != null)
            {
                Triplet t = (Triplet)state;

                Clear();
                string[] rgUrl = t.First.Cast<string[]>();
                string[] rgText = t.Second.Cast<string[]>();
                string[] rgTooltip = t.Third.Cast<string[]>();
                for (int i = 0; i < rgUrl.Length; i++)
                    Add(new HyperLinkItem(rgText[i], rgUrl[i], rgTooltip[i]));
            }
        }

        public object SaveViewState()
        {
            int numOfItems = Count;
            object[] rgTooltip = new string[numOfItems];
            object[] rgText = new string[numOfItems];
            object[] rgUrl = new string[numOfItems];
            for (int i = 0; i < numOfItems; i++)
            {
                rgTooltip[i] = this[i].Tooltip;
                rgText[i] = this[i].Text;
                rgUrl[i] = this[i].Url;
            }
            return new Triplet(rgUrl, rgText, rgTooltip);
        }

        public void TrackViewState()
        {
            _marked = true;
        }

        #endregion

    }
}
